package com.ss.mq.pattern.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.ss.mq.util.RabbitUtils;

import java.util.Scanner;

/**
 * @author 叁昇
 * @create 2020-12-12-15:44
 */
public class WorkQueueProducer {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("FinalWorkQueue", true, false, false, null);

        while (true) {
            // 控制台输入的信息发送到 mq
            System.out.print("请输入消息: ");
            String msg = new Scanner(System.in).nextLine();
            // 若输入 "exit" 则结束进程
            if ("exit".equals(msg)) {
                break;
            }
            channel.basicPublish("", "FinalWorkQueue", MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
            System.out.println("消息已发送: [ "+msg+" ]");
        }

        channel.close();
    }
}
